package com.sw.chinesewriteboard.activity;

import android.os.Bundle;
import android.os.Environment;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import com.gyf.barlibrary.ImmersionBar;
import com.sw.chinesewriteboard.R;
import com.sw.chinesewriteboard.adapter.FileListAdapter;
import com.sw.chinesewriteboard.databinding.ActivityListBinding;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @author: GordenGao
 * @Email: gordengao124@gmail.com
 * @date: 2021年06月24日 20:53
 */
public class ListActivity extends AppCompatActivity {

    private FileListAdapter fileListAdapter;
    ActivityListBinding binding;
    private List<File> mData = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ImmersionBar.with(this).transparentStatusBar().statusBarDarkFont(true).init();
        binding = ActivityListBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        fileListAdapter = new FileListAdapter(R.layout.mine_photot_item, mData);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);

        binding.listRv.setAdapter(fileListAdapter);
        binding.listRv.setLayoutManager(gridLayoutManager);
        binding.detailBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        updatePictureList();
        fileListAdapter.notifyDataSetChanged();
    }

    private void updatePictureList() {
        mData.clear();
        String saveDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath();

        File dir = new File(saveDir);
        if (dir.exists()) {
            File[] listFiles = dir.listFiles();
            for (File file : listFiles) {
                if(file.exists() && file.length() > 0){
                    mData.add(file);
                }
            }

            if (mData.size() > 0) {
                fileListAdapter.notifyDataSetChanged();
            }
        }
    }
}
